﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_KQKT
{
    public partial class BaoCaoMau5 : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            if (!IsPostBack)
            {
                BindDataToControls();
                if (Request.QueryString["KyBaoCao"] != null)
                {
                    cboKyBaoCao.SelectedValue = Request.QueryString["KyBaoCao"];
                    cboKyBaoCao_SelectedIndexChanged(sender, e);
                }
            }



        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau5");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {
                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];
                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
            }
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT CM_TenBaoCao,CreateDate,UpdateDate FROM dbo.tblBaoCaoMau5 WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtSoBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtFrom.Text = Convert.ToDateTime(dt.Rows[0]["CreateDate"]).ToString("dd/MM/yyyy");
                txtTo.Text = Convert.ToDateTime(dt.Rows[0]["UpdateDate"]).ToString("dd/MM/yyyy");
            }
        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, Session["userdonvi"]));
            lstParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text,
                "SELECT M1.*,A.TenDM,A.BoiDen,A.STT " +
                "FROM dbo.tblBaoCaoMau5 M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                "WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID", lstParams);
            grvData.DataSource = dt;
            grvData.DataBind();
            btnLuu.Visible = true;
            grvData.Visible = true;
        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = false;
                grvData.Visible = false;
                grvData.EditIndex = -1;
                BindDataToGrid();
                if (((DataTable)grvData.DataSource).Rows.Count == 0)
                {
                    SqlTransaction MyTran = SqlHelper.BeginTransaction();
                    try
                    {
                        List<SqlParameter> lstParams = new List<SqlParameter>();
                        lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                        lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, Session["userdonvi"]));
                        lstParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                        string sql = "INSERT INTO dbo.tblBaoCaoMau5( KyBaoCaoID_DM ,DonViID ,TenBaoCaoID_DM ,F1 ,F2 ,F3 ,F4 ,F5 ,F6 ,F7 , " +
                                 "F8 ,F9 ,F10 ,F11 ,F12 ,F13 ,F14 ,F15 ,F16 ,F17 ,F18 ,F19 ,F20 ,F21 ,F22 ,F23 ," +
                                 "F24 ,F25 ,F26 ,F27 ,F28 ,F29 ,F30 ,F31 ,F32 ,F33 ,F34 ,F35 ,F36 ,F37 ,F38 ,F39 ,   " +
                                 "F40 ,F41 ,F42 ,F43 ,F44 ,F45 ,F46 ,F47 ,F48 ,UserID)   " +
                                 "SELECT @KyBaoCaoID_DM ,@DonViID ,B.ID,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"+
                                 "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,@UserID FROM dbo.tblDanhMucBaoCao A " +
                                 "INNER JOIN dbo.tblDanhMucBaoCao B ON A.ID = B.FatherID WHERE A.TenDM != B.TenDM AND B.MaDM = 95 " +
                                 "GROUP BY A.TenDM,B.TenDM,A.ID,B.ID ORDER BY A.ID,B.ID";
                        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql, lstParams);
                        sql = "INSERT INTO dbo.tblBaoCaoMau5(KyBaoCaoID_DM,DonViID,IsCommon ,UserID) " +
                              "VALUES( @KyBaoCaoID_DM,@DonViID,1,@UserID)";
                        SqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql, lstParams);
                        MyTran.Commit();
                    }
                    catch
                    {
                        MyTran.Rollback();
                    }
                    grvData.EditIndex = -1;
                    BindDataToGrid();
                }
                else
                {
                    BindDataToTextField();
                    grvData.Visible = true;
                }
            }
            else
            {
                btnLuu.Visible = false;
                grvData.Visible = false;

            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

            string sSQL = "Update tblBaoCaoMau5 Set CM_SoBaoCao=@SoBaoCao,CM_TenBaoCao=@TenBaoCao,CreateDate=CONVERT(DATETIME,@TuNgay,103),UpdateDate=CONVERT(DATETIME,@DenNgay,103)" +
                            "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(SqlHelper.CreateParameterObject("@SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtSoBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@TuNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtFrom.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@DenNgay", SqlDbType.NVarChar, ParameterDirection.Input, txtTo.Text.Trim()));


            SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

            MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);

        }

        #endregion Events


        #region GridCommand

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow1 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow4 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 4, "Số thứ tự");
                AddMergedCells(objgridviewrow, objtablecell, 1, 4, "");
                AddMergedCells(objgridviewrow, objtablecell, 20, 1, "C - KIỂM TRA TÀI CHÍNH ĐẢNG");
                AddMergedCells(objgridviewrow, objtablecell, 12, 1, "D - KIỂM TRA TỔ CHỨC ĐẢNG CẤP DƯỚI THỰC HIỆN NHIỆM VỤ KIỂM TRA, GIÁM SÁT");
                AddMergedCells(objgridviewrow, objtablecell, 16, 1, "E - KIỂM TRA VIỆC THI HÀNH KỶ LUẬT CỦA TCĐ CẤP DƯỚI");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow1, objtablecell, 1, 3, "Tổng số tổ chức được kiểm tra");
                AddMergedCells(objgridviewrow1, objtablecell, 5, 1, "CẤP KIỂM TRA");
                AddMergedCells(objgridviewrow1, objtablecell, 8, 1, "KẾT QUẢ KIỂM TRA TỔ CHỨC");
                AddMergedCells(objgridviewrow1, objtablecell, 6, 1, "KẾT QUẢ KIỂM TRA ĐẢNG VIÊN");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 3, "Tổng số tổ chức được kiểm tra,giám sát");
                AddMergedCells(objgridviewrow1, objtablecell, 5, 1, "CẤP KIỂM TRA");
                AddMergedCells(objgridviewrow1, objtablecell, 2, 1, "NỘI DUNG KIỂM TRA");
                AddMergedCells(objgridviewrow1, objtablecell, 4, 1, "KẾT LUẬN");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 3, "Tổng số tổ chức được kiểm tra");
                AddMergedCells(objgridviewrow1, objtablecell, 5, 1, "CẤP KIỂM TRA");
                AddMergedCells(objgridviewrow1, objtablecell, 9, 1, "ĐẢNG VIÊN BỊ THKL TRONG MỖ THỜI GIAN KT");
                AddMergedCells(objgridviewrow1, objtablecell, 1, 3, "Số ĐV có VP đến mức phải THKL, nhưng không XLKL");
                objgridviewrow1.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow1);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKTĐU t.thuộc QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cấp trên t.tiếp cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Đảng ủy bộ phận");
                AddMergedCells(objgridviewrow2, objtablecell, 3, 1, "T.chức có VP về ngân sách Đảng");
                AddMergedCells(objgridviewrow2, objtablecell, 3, 1, "T.chức có VP về đảng phí");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Truy thu");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổng số đảng viên được kiểm tra");
                AddMergedCells(objgridviewrow2, objtablecell, 3, 1, "Trong đó");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Truy thu");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKTĐU t.thuộc QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cấp trên t.tiếp cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Đảng ủy bộ phận");

                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Việc XD C.trình KH và L.đạo hoạt động kiểm tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổ chức thực hiện nhiệm vụ kiểm tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Số t/c có c.trình kế hoạch và l.đạo hoạt động k.tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Số t/c đã thực hiện kế hoạch kiểm tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Số t/c thực hiện tốt công tác kiểm tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Số t/c chưa thực hiện tốt c.tác kiểm tra, giám sát");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKTĐU t.thuộc QUTW");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cấp trên t.tiếp cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "UBKT ĐU cơ sở");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Đảng ủy bộ phận");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổng số");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Trong đó số có k.nại sau khi bị thi hành kỷ luật");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Tổng số đảng viên được kiểm tra");
                AddMergedCells(objgridviewrow2, objtablecell, 6, 1, "Kết luận");

                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số vi phạm");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Phải thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Đã thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số vi phạm");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Phải thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Đã thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số TC Đảng");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số tiền truy thu (nghìn đồng)");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số có vi phạm");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Phải thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Đã thi hành KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tổng số đ.viên");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số tiền truy thu (nghìn đồng)");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Giữ nguyên mức KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tăng mức KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Giảm mức KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Xóa bỏ KL");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số bị THKL không đúng thẩm quyền");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số bị THKL chưa đúng nguyên tắc, thủ tục");

                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(3, objgridviewrow3);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "A");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "B");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "16");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "17");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "18");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "19");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "20");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "21");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "22");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "23");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "24");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "25");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "26");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "27");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "28");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "29");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "30");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "31");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "32");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "33");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "34");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "35");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "36");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "37");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "38");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "39");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "40");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "41");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "42");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "43");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "44");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "45");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "46");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "47");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "48");

                objgridviewrow4.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(4, objgridviewrow4);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                string[] BoiDen = dtr.Row.ItemArray[65].ToString().Split(',');
                if (BoiDen.Length >= 1)
                {
                    if (!BoiDen[0].Equals(""))
                    {
                        for (int i = 0; i < BoiDen.Length; i++)
                        {

                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].BackColor = System.Drawing.Color.LightGray;
                            e.Row.Cells[Convert.ToInt32(BoiDen[i]) + 1].Enabled = false;
                        }
                    }
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == AIC_KQKT.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                for (int i = 3; i <= 24; i++)
                {
                    if (i != 21)
                    {
                        TextBox txtField = (TextBox)grvData.Rows[grvData.EditIndex].Cells[i].Controls[0];
                        arrParams.Add(SqlHelper.CreateParameterObject("F" + (i - 1), SqlDbType.Float, ParameterDirection.Input, txtField.Text));
                    }
                }
                string sql = "UPDATE tblBaoCaoMau5 SET [F1]=@F2+@F3+@F4+@F5+@F6+@F7,[F2]=@F2,[F3]=@F3," +
                             "[F4]=@F4,[F5]=@F5,[F6]=@F6,[F7]=@F7,[F8]=@F8,[F9]=@F9,[F10]=@F10,[F11]=@F11," +
                             "[F12]=@F12,[F13]=@F13,[F14]=@F14,[F15]=@F15,[F16]=@F16,[F17]=@F17,[F18]=@F18," +
                             "[F19]=@F19,[F20]=@F8+@F9+@F10+@F11+@F12+@F13+@F14+@F15+@F16+@F17+@F18+@F19,[F21]=@F21,[F22]=@F22," +
                             "[F23]=@F23,[UpdateDate]=GETDATE(),[UserID]=@UserID WHERE ID=@ID ";
                arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(sID)));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
                // cap nhat vao database

                grvData.EditIndex = -1;
                BindDataToGrid();
                MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData.EditIndex = e.NewEditIndex;
            grvData.Rows[grvData.EditIndex].Cells[3].Controls.Clear();
            BindDataToGrid();
        }



        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand

        protected void grvData_PreRender(object sender, EventArgs e)
        {
           
        }

        protected void imgNext_Click(object sender, ImageClickEventArgs e)
        {
            //Response.Redirect("BaoCaoMau1_B.aspx");
            Response.Redirect("BaoCaoMau4_B.aspx?KyBaoCao=" + cboKyBaoCao.SelectedValue);
        }
    }
}
